package butines.game.state;

import butines.core.Sprite;
import butines.core.game.fsm.StateMachine;
import butines.game.Unit;

public class AttackState extends AbstractState {

	private static final AttackState instance = new AttackState();
	
	public static AttackState getInstance() {
		return instance;
	}
	
	@Override
	public void enter(Unit unit) {
		
	}

	@Override
	public void leave(Unit unit) {
		
	}

	@Override
	public void execute(Unit unit) {
		Sprite sprite = unit.getSprite();
		if (sprite.isAnimation("attack")) {
			return;
		}
		
		StateMachine<Unit> stateMachine = unit.getStateMachine();
		Unit enemy = unit.getEnemy();
		
		if (enemy == null || enemy.isDead()) {
			stateMachine.changeState(StayState.getInstance());
			return;
		}
				
		if (!unit.isNearToEnemy()) {
			stateMachine.changeState(ChaseToEnemyState.getInstance());
			return;
		}
		
		unit.setSpeed(0);
		unit.attack();
	}
	
}
